package org.easymis.oa.modules.main.controller;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.easymis.commons.web.page.SimplePage;
import org.easymis.entitys.mybatis.dto.AssetAddress;
import org.easymis.entitys.mybatis.dto.CmsSite;
import org.easymis.entitys.mybatis.dto.Member;
import org.easymis.entitys.mybatis.dto.OrganizeFunction;
import org.easymis.oa.service.asset.AssetAddressMng;
import org.easymis.oa.service.datacenter.OrganizeFunctionMng;
import org.easymis.oa.web.commons.EasymisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/consumable/")
public class ConsumableAction {
	@Autowired
	OrganizeFunctionMng organizeFunctionMng;
	@Autowired
	private AssetAddressMng service;
	@RequestMapping(value = "index")
	public String forwardaddressareaIndexJsp(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		
		model.put("breadcrumbList", breadcrumbList);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		return "asset/address/index";
	}
	@RequestMapping(value = "grid", method = RequestMethod.POST)
	public String forwardList(Integer page, Integer pageSize, HttpSession session,HttpServletRequest request) throws Exception {
		Member user =(Member)session.getAttribute("userInfo");
		AssetAddress bean = new AssetAddress();
		bean.setOrgId(user.getOrgId());
		PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
		List<AssetAddress> brandList=service.getList(bean);
		PageInfo<AssetAddress> p = new PageInfo<AssetAddress>(brandList);
		request.setAttribute("brandList", brandList);
		request.setAttribute("pageTotalResult", p.getTotal());
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		return "asset/address/grid";
	}
	@RequestMapping(value = "query")
	public String query(String prodStatus,String prodCatId, Integer page, Integer pageSize,
			HttpServletRequest request, HttpServletResponse response, ModelMap model, HttpSession session) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user = (Member) session.getAttribute("userInfo");
		PageHelper.startPage(SimplePage.cpn(page), 999);
		AssetAddress param = new AssetAddress();
		param.setOrgId(user.getOrgId());
		List<AssetAddress> brandList = service.getList(param);
		PageInfo<AssetAddress> p = new PageInfo<AssetAddress>(brandList);
		model.put("brandList", brandList);
		
		return "/asset/address/brand_list";
	}
	@RequestMapping(value = "ajax/query")
	@ResponseBody
	public Map<String, Object> queryByAjax( String customerName,Integer page, Integer pageSize,
			HttpServletRequest request, HttpServletResponse response, ModelMap model, HttpSession session) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user =(Member)session.getAttribute("userInfo");
		PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
		AssetAddress param = new AssetAddress();
		param.setOrgId(user.getOrgId());
		List<AssetAddress> brandList = service.getList(param);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("brandList", brandList);
		return result;
	}

	@RequestMapping(value = "forwordAdd")
	public String add(
			HttpServletRequest request, HttpServletResponse response, ModelMap model, HttpSession session) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user = (Member) session.getAttribute("userInfo");


		request.setAttribute("base", "http://demo.easymis.cn/oa");
		return "asset/address/add";
	}
	@RequestMapping(value = "forworUpdate")
	public String edit(String brandId, 
			HttpServletRequest request, HttpServletResponse response, HttpSession session,ModelMap model) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user =(Member)session.getAttribute("userInfo");

		AssetAddress bean = service.findById(brandId);
		model.put("brand", bean);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		return "asset/address/edit";
	}
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public void save(
			HttpServletRequest request, HttpServletResponse response, ModelMap model,HttpSession session,AssetAddress bean) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user =(Member)session.getAttribute("userInfo");
		
		if (user != null) {
			bean.setOrgId(user.getOrgId());
			service.save(bean);
		}
	}
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public void update(
			HttpServletRequest request, HttpServletResponse response,  HttpSession session,AssetAddress bean,ModelMap model) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user =(Member)session.getAttribute("userInfo");
		
		if (null!=user) {
			service.update(bean);
		}

	}
	@RequestMapping(value = "delete")
	public String delete(String brandId,
			HttpServletRequest request, HttpServletResponse response, ModelMap model, HttpSession session) {
		CmsSite site = EasymisUtils.getSite(request);
		Member user = (Member) session.getAttribute("userInfo");
		

		service.delete(brandId);
		
		return "redirect:/asset/address/query";
	}

	@RequestMapping(value = "deleteByIds", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	@ResponseBody
	public String deleteByIds(HttpServletRequest request) throws Exception {
		String ids_str = request.getParameter("id");
		String[] ids = ids_str.split(",");
		//service.deleteByIds(ids);

		return "";

	}

	/**
	 * 
	 * @Title: 数据导出到excel @Description: TODO(这里用一句话描述这个方法的作用) @param @param
	 *         response @param @param request @param @throws Exception 设定文件 @return
	 *         void 返回类型 @throws
	 */
	@RequestMapping(value = "exportDate2Excel", method = RequestMethod.GET)
	@ResponseStatus(value = HttpStatus.OK)
	public void exportDate2Excel(HttpServletResponse response, HttpServletRequest request) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String filename = "商品类别.xls";
		String title = filename.replace(".xls", "");
	}

	/**
	 * 下载Excel模板
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "loadExcelTemplate", method = RequestMethod.GET)
	@ResponseStatus(value = HttpStatus.OK)
	public void loadExcelTemplate(HttpServletResponse response, HttpSession session) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 模板路径
		String filepath = session.getServletContext().getRealPath("/upload/customer");
		// 模板文件goodsTemplate.xls
		File goodsTemplate = new File(filepath + "/customerTemplate.xls");
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(goodsTemplate));
		// 读取模板文件
		HSSFWorkbook workbook = new HSSFWorkbook(fs);
		HSSFCell cell = null;

	}

	/**
	 * 导入Excel数据到数据库
	 * 
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping(value = "importExcel2Database", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void importExcel2Database(HttpSession session, HttpServletRequest request,
			@RequestParam("upload") MultipartFile file) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		Date date = sdf.parse(sdf.format(new Date()));
		// Excel工作簿
		HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
		HSSFSheet sheet = workbook.getSheetAt(0);
		HSSFRow row = null;
	}


}
